﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;



namespace MSOfficeAutoConverter
{
    class AutoAgent
    {

        public static void Main2()
        {
            Object missing = System.Reflection.Missing.Value;

            //Objects for true and false to be used in the word document for passing true or false.
            Object istrue = true;
            Object oFalse = false;
            Object oFalse2 = false;
            //Creating objects of word and document
            _Application oWord = new Application();
            _Document oWordDoc = new Document();
            _Application oWord2 = new Application();
            _Document oWordDoc2 = new Document();
            //Adding a new document to the application

            oWordDoc = oWord.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            oWordDoc2 = oWord2.Documents.Add(ref missing, ref missing, ref missing, ref missing);

            // To open an existing word document we need to pass the path of the existing document with some // other parameters. Here is the code snippet opening an existing word document

            object fileName = @"d:\temp\o2003PIA_ReadMe.rtf";
            //fileName = @"http://www.ieee.org/portal/cms_docs_iportals/iportals/publications/rights/IEEECopyrightForm.doc";
            // You can keep it true if you want to open the file in readonly mode
            object readOnly = true;
            // we can keep it false if you want to open the file but not make it invisible
            object isVisible = false;
            object openAndRepair = false;
            object noEncodingDialog = true;


            /*
            [In] ref object FileName, 
	        [In, Optional] ref object ConfirmConversions, 
	        [In, Optional] ref object ReadOnly, 
	        [In, Optional] ref object AddToRecentFiles, 
	        [In, Optional] ref object PasswordDocument, 
	        [In, Optional] ref object PasswordTemplate, 
	        [In, Optional] ref object Revert, 
	        [In, Optional] ref object WritePasswordDocument, 
	        [In, Optional] ref object WritePasswordTemplate, 
	        [In, Optional] ref object Format, 
	        [In, Optional] ref object Encoding, 
	        [In, Optional] ref object Visible, 
	        [In, Optional] ref object OpenAndRepair, 
	        [In, Optional] ref object DocumentDirection, 
	        [In, Optional] ref object NoEncodingDialog, 
	        [In, Optional] ref object XMLTransform
            */
            oWordDoc = oWord.Documents.Open(
                ref fileName,
                ref missing,
                ref readOnly, //yes
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref isVisible, //false
                ref openAndRepair, //false
                ref missing,
                ref noEncodingDialog, //true
                ref missing);
            oWordDoc2 =
                oWord2.Documents.Open(
                ref fileName,
                ref missing,
                ref readOnly, //yes
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref isVisible, //false
                ref openAndRepair, //false
                ref missing,
                ref noEncodingDialog, //true
                ref missing);
            object newF = @"d:\temp\o2003PIA_ReadMe4.txt";
            object newF2 = @"d:\temp\o2003PIA_ReadMe4.pdf";
            object format = WdSaveFormat.wdFormatUnicodeText;
            object format2 = WdSaveFormat.wdFormatPDF;
            //format2 = WdSaveFormat.wdFormatXPS;

            oWordDoc2.SaveAs(
              ref newF2, ref format2, ref  

                  readOnly, ref missing, ref missing, ref missing, ref  

                  missing, ref missing, ref missing, ref missing, ref 

                  missing, ref isVisible, ref missing, ref missing, ref 

                  missing, ref missing);
            oWordDoc2.Close(ref oFalse2, ref missing, ref missing);


            oWordDoc.SaveAs(
                ref newF, ref format, ref  

                  readOnly, ref missing, ref missing, ref missing, ref  

                  missing, ref missing, ref missing, ref missing, ref 

                  missing, ref isVisible, ref missing, ref missing, ref 

                  missing, ref missing);
            //Closing the file

            oWordDoc.Close(ref oFalse, ref missing, ref missing);

            //Quitting the word application to release the memory.

            oWord.Quit(ref missing, ref missing, ref missing);
            oWord2.Quit(ref missing, ref missing, ref missing);


            //System.Threading.Thread.CurrentThread.Join();
        }


    }

}
